using System;
using System.IO;

using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

using Pronghorn.Core;

namespace ViewEngine_Tests
{
	[TestFixture]
	public class FilesTags_Tests
	{
		[Test]
		public void When_a_null_path_is_passed_it_should_return_an_empty_string()
		{
			Uri uri = null;
			FilesTagBase tag = new JavaScriptTag(uri);

			Assert.IsEmpty(tag.Code);
		}

		[Test]
		public void When_a_path_is_given_we_should_get_a_hash_key()
		{
			FilesTagBase tag = new JavaScriptTag(new Uri("pc/lib/Company/UI/UI.js", UriKind.RelativeOrAbsolute));
			var result = tag.GetUniqueKey();

			Assert.IsNotEmpty(result);
		}

		[Test]
		public void When_a_path_is_null_it_should_not_render()
		{
			Uri uri = null;
			FilesTagBase tag = new JavaScriptTag(uri);
			var result = new StringWriter();
			tag.Render(result);

			Assert.IsEmpty(result.ToString());
		}

		[Test]
		public void When_a_path_is_passed_but_no_file_is_found_it_should_return_an_empty_string()
		{
			FilesTagBase tag = new JavaScriptTag(new Uri("some/uri/file.js",UriKind.RelativeOrAbsolute));
			Assert.IsEmpty(tag.Code);
		}

		[Test]
		public void When_a_path_is_passed_calling_Code_should_return_the_code_of_the_file()
		{
			FilesTagBase tag = new JavaScriptTag("alert('This is an alert message.');");
			const string expected = "alert('This is an alert message.');";

			Assert.That(tag.Code[0], Is.EqualTo(expected));
		}

		[Test]
		public void When_a_path_is_passed_should_render_a_script_with_a_src_attribute()
		{
			FilesTagBase tag = new JavaScriptTag(new Uri("../scripts/libs/Company/UI/Ui.js",UriKind.RelativeOrAbsolute));
			var result = new StringWriter();
			tag.Render(result);

			const string expected = "<script type=\"text/javascript\" src=\"../scripts/libs/Company/UI/Ui.js\"></script>\r\n";
			Assert.That(result.ToString(),Is.EqualTo(expected));
		}

		[Test]
		public void When_code_is_passed_should_render_an_inline_script_tag()
		{
			FilesTagBase tag = new JavaScriptTag("alert('Hello world from the render method.');\r\nalert('more');");
			var result = new StringWriter();
			tag.Render(result);

			const string expected = "<script type=\"text/javascript\">\r\nalert('Hello world from the render method.');\r\nalert('more');\r\n</script>\r\n";

			Assert.That(result.ToString(), Is.EqualTo(expected));
		}

		[Test]
		public void When_code_is_passed_we_should_get_a_hash_key()
		{
			FilesTagBase tag = new JavaScriptTag("alert('This is an alert message.');");
			var result = tag.GetUniqueKey();

			Assert.IsNotEmpty(result);
		}

		[Test]
		public void When_the_code_is_empty_it_should_not_render()
		{
			FilesTagBase tag = new JavaScriptTag("");
			var result = new StringWriter();
			tag.Render(result);

			const string expected = "";

			Assert.That(result.ToString(),Is.EqualTo(expected));
		}

		[Test]
		public void When_the_code_is_empty_it_should_return_an_empty_string()
		{
			FilesTagBase tag = new JavaScriptTag("");
			Assert.IsEmpty(tag.Code);
		}

		[Test]
		public void When_the_code_is_null_it_should_not_render()
		{
			string code = null;
			FilesTagBase tag = new JavaScriptTag(code);
			var result = new StringWriter();
			tag.Render(result);

			Assert.IsEmpty(result.ToString());
		}

		[Test]
		public void When_the_code_is_null_it_should_return_an_empty_string()
		{
			string code = null;
			FilesTagBase tag = new JavaScriptTag(code);

			Assert.IsEmpty(tag.Code);
		}

		[Test]
		public void When_two_identical_codes_are_passed_they_should_have_the_same_key()
		{
			FilesTagBase tag = new JavaScriptTag("alert('This is an alert message.');");
			FilesTagBase tag2 = new JavaScriptTag("alert('This is an alert message.');");
			var result = tag.GetUniqueKey();
			var result2 = tag2.GetUniqueKey();

			Assert.That(result,Is.EqualTo(result2));
		}

		[Test]
		public void When_we_pass_code_calling_Code_should_return_it()
		{
			FilesTagBase tag = new JavaScriptTag("alert('This is an alert message.');");
			const string expected = "alert('This is an alert message.');";

			Assert.That(tag.Code[0],Is.EqualTo(expected));
		}
	}
}